/* --------------------------------------
SUMMARY: 	- Estimating discete choice models
			- Exporting the estimated parameters to a new dataset
AUTHOR: Thimo De Schouwer (KU LEUVEN)
NOTES: 		- Preference / WTP coefficients vary over wage distribution
DATE CREATED: 16/09/2021
STATA VERSION: MP 17.0
--------------------------------------- */

clear all
drop _all

* Part 2 - Estimating preferences for job attributes
cd "${dir}\DataCode\Data\Constructed\ISSP"
import delimited Clean_ISSP.txt, clear


* Renaming as in LISS
rename children child
gen children = (child == "TRUE")
drop child
rename children child

gen geslacht = (woman == "TRUE")
replace geslacht = geslacht + 1

rename age leeftijd
rename high_flex flex_d
rename high_tel tel_d
rename high_meaning meaning_d

rename married marriage
gen married = 0 if marriage == "FALSE"
replace married = 1 if marriage == "TRUE"

* Education: 
gen educ_class = 1 if education <= 11
	replace educ_class = 2 if education >= 12 & education <= 16
	replace educ_class = 3 if education >= 17 & education != .

	drop education
	rename educ_class education

*** Part 0 - DEMOGRAPHICS and AMENITIES by GENDER
** Section 0.1 - DEMOGRAPHICS
* Generating means / sd by gender (of binary variables)
foreach i in married leeftijd child flex_d tel_d meaning_d education{
	summ `i' [aweight = weight]
	gl mean_`i'0 	= r(mean)
	gl sd_`i'0 		= r(sd)
	
	ttest `i' , by(geslacht)
	gl mean_`i'1 	= r(mu_1)
	gl sd_`i'1		= r(sd_1) 		// NB: Tab s.d. only for age
	gl mean_`i'2 	= r(mu_2)
	gl sd_`i'2 		= r(sd_2)
	gl pval_`i' 	= r(p)
	
	* Counts: observations & nr_(wo)men
	gl nr_men = r(N_1)
	gl nr_women = r(N_2)
	summ married, det
	gl nr_tot = r(N)
}

* Generating mean / sd by gender (of categorical variables)
forvalues i = 1/3{
	gen education`i' = 1 if education == `i'
		replace education`i' = 0 if education`i' == .

	ttest education`i', by(geslacht)
		gl pval_education_`i' = r(p)
	
	count if education`i' == 1
	gl education_perc_tot_`i' = r(N) / $nr_tot
	
	count if education`i' == 1 & geslacht == 1
	gl education_perc_men_`i' = r(N) / $nr_men
	
	count if education`i' == 1 & geslacht == 2
	gl education_perc_women_`i' = r(N) / $nr_women
}

* Prepare for latex export
matrix define sum_general = (round($nr_tot, 1), round($nr_men, 1), round($nr_women, 1), . \ ///
							round($mean_leeftijd0, .01), round($mean_leeftijd1, .01), round($mean_leeftijd2, .01), round($pval_leeftijd, .01)  \ ///
							round($sd_leeftijd0, .01), round($sd_leeftijd1, .01), round($sd_leeftijd2, .01), . \ ///
							round($mean_married0, .01), round($mean_married1, .01), round($mean_married2, .01), round($pval_married, .01)  \ ///
							round($mean_child0, .01), round($mean_child1, .01), round($mean_child2, .01), round($pval_child, .01)  \ ///
							round($mean_education0, .01), round($mean_education1, .01), round($mean_education2, .01), round($pval_education, .001) \ ///
							round($sd_education0, .01), round($sd_education1, .01), round($sd_education2, .01), . \ ///
							round($mean_flex_d0, .01), round($mean_flex_d1, .01), round($mean_flex_d2, .01), round($pval_flex_d, .01)  \ ///
							round($sd_flex_d0, .01), round($sd_flex_d1, .01), round($sd_flex_d2, .01), . \ ///
							round($mean_tel_d0, .01), round($mean_tel_d1, .01), round($mean_tel_d2, .01), round($pval_tel_d, .01)  \ ///
							round($sd_tel_d0, .01), round($sd_tel_d1, .01), round($sd_tel_d2, .01), . \ ///
							round($mean_meaning_d0, .01), round($mean_meaning_d1, .01), round($mean_meaning_d2, .01), round($pval_meaning_d, .01)  \ ///
							round($sd_meaning_d0, .01), round($sd_meaning_d1, .01), round($sd_meaning_d2, .01), . )
 		
mat coln sum_general = "Total" "Men" "Women" "P-value"
mat rown sum_general = "Observations: Number of obs." "Age: Mean" "Age: Std dev" "Family: Married (\%)" "Family: Children (\%)" ///
						"Education: Years of Schooling (mean)" "Education: Years of Schooling (std dev)"  ///
						"Amenities: Schedule Adaptability (mean)"  "Amenities: Schedule Adaptability (std dev)" ///
						"Amenities: Telecommuting (mean)" "Amenities: Telecommuting (std dev)" ///
						"Amenities: Meaning (mean)" "Amenities: Meaning (std dev)" 

esttab matrix(sum_general, fmt(2)) using "$dir\GraphsTables\OnlineAppendix\Table_BackgroundStats_ISSP.txt", tex varwidth(20) modelwidth(10) ///
    replace nomtitle nogaps nofloat
	
** Number of Observations
encode country_s, gen(country_s_lab)
estpost tabstat country_s_lab, by(country_s_lab) stat(n) col(stat)
matrix anys = e(count)'
esttab matrix(anys) using "$dir\GraphsTables\OnlineAppendix\Table_Countries_ISSP.txt", tex  varwidth(20) modelwidth(10) ///
    replace nomtitle nogaps nofloat
	
	
